<%@ Page language="vb" CodeFile="searchInvoicePenalty.aspx.vb" Inherits="searchInvoicePenalty_aspx_vb" %>
<%
    rowNumber = 1
    invoiceCount = 0
    If (Request.QueryString("do")) = Nothing Then 
        subDo = Request.QueryString("subDo")
        dbOpen()
        RecSet = Conn.Execute("Select partnerID, partnerName, countryID From tblPartner Order by partnerName")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                partnerID = RecSet.Fields("partnerID").Value
                partnerName = RecSet.Fields("partnerName").Value
                countryID = RecSet.Fields("countryID").Value
                strSelectSubJS = strSelectSubJS + "new Array(" & partnerID & "," & countryID & ",""" & partnerName & """),"
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        dbOpen()
        RecSet = Conn.Execute("Select countryID From tblCountry Order by countryID")
        Do Until RecSet.EOF
            countryID = RecSet.Fields("countryID").Value
            strSelectSubJS = strSelectSubJS + "new Array(0," & countryID & ","" ------------------------------------  ""),"
            RecSet.MoveNext()
        Loop
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        strSelectSubJS = Left(strSelectSubJS, Len(strSelectSubJS) - 1)
        strSelectSubHeadline = "Select lease taker"
        selectSub()
        Over()
        strJS = ""
        strJS = strJS + "[""invoiceType"", ""<1""]"
        checkFields()
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='100%'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>SEARCH PENALTY INVOICE<hr class='hrHeadline'></td></tr>")
        Response.Write("<form action='searchInvoicePenalty.aspx?subDo=search' method='post' name='store_productsSearch' onsubmit=""javascript:return checkFields();"">")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Type of invoice:</td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='invoiceType' id='invoiceType'>")
        Response.Write("<option value=''> - Select invoice type - ")
        Response.Write("<option value='1'> Contract cancellation ")
        Response.Write("<option value='2'> Meter reading overdue ")
        Response.Write("<option value=''> -------------------------------------- ")
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("<td><div id='error_invoiceType' class='edit_error'>&laquo; OBS!</div></td>")
        Response.Write("</tr>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Country:</td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='countryID' id='countryID' onchange=""set_child_listbox(this, document.store_productsSearch.partnerID,Product,'Products');"">")
        Response.Write("<option value=''> - Select country - ")
        dbOpen()
        RecSet = Conn.Execute("Select countryID, country From tblCountry")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                countryID = RecSet.Fields("countryID").Value
                country = RecSet.Fields("country").Value
                Response.Write("<option value='" & countryID & "'> " & country & " ")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<option value=''> -------------------------------------- ")
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Partner:</td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='partnerID' id='partnerID' disabled>")
        Response.Write("<option value=''> - Select partner - ")
        Response.Write("<option value=''> -------------------------------------- ")
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("<tr><td class='formBold'>Invoice ID:</td><td align='right'><input type='text' name='invoicePenaltyID' id='invoicePenaltyID' size='40' maxlength='50' onkeyup='re=/(\d*)/; re.exec(this.value); this.value=RegExp.$1;'></td></tr>")
        Response.Write("<tr><td class='formBold'>Invoice date (YYYY-MM-DD):</td><td align='right'><input type='text' name='invoiceDateFrom' id='invoiceDateFrom' size='17' maxlength='10'onkeyup='re=/([\d\-]*)/; re.exec(this.value); this.value=RegExp.$1;'> - <input type='text' name='invoiceDateTo' id='invoiceDateTo' size='17' maxlength='10' onkeyup='re=/([\d\-]*)/; re.exec(this.value); this.value=RegExp.$1;'></td></tr>")
        Response.Write("<tr><td class='formBold'>Payment date (YYYY-MM-DD):</td><td align='right'><input type='text' name='paymentDateFrom' id='paymentDateFrom' size='17' maxlength='10'onkeyup='re=/([\d\-]*)/; re.exec(this.value); this.value=RegExp.$1;'> - <input type='text' name='paymentDateTo' id='paymentDateTo' size='17' maxlength='10' onkeyup='re=/([\d\-]*)/; re.exec(this.value); this.value=RegExp.$1;'></td></tr>")
        Response.Write("<tr><td class='formBold'>Licence number:</td><td align='right'><input type='text' name='regnr' id='regnr' size='40' maxlength='50'></td></tr>")
        Response.Write("<tr><td class='formBold'>VIN-number:</td><td align='right'><input type='text' name='vinNumber' id='vinNumber' size='40' maxlength='50'></td></tr>")
        Response.Write("<tr><td align='right' colspan='2'><input type='submit' value='Search'></td></tr>")
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("<td width='100'></td>")
        Response.Write("<td valign='top'>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("<tr><td><br></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td valign='top' colspan='3'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='100%'>")
        Response.Write("<tr><td class='headline' colspan='14'>SEARCH RESULT<hr class='hrHeadline'></td></tr>")
        invoiceType = Request.Form("invoiceType")
        countryID = Request.Form("countryID")
        partnerID = Request.Form("partnerID")
        invoicePenaltyID = Request.Form("invoicePenaltyID")
        invoiceDateFrom = Request.Form("invoiceDateFrom")
        invoiceDateTo = Request.Form("invoiceDateTo")
        paymentDateFrom = Request.Form("paymentDateFrom")
        paymentDateTO = Request.Form("paymentDateTo")
        regnr = Request.Form("regnr")
        vinNumber = Request.Form("vinNumber")
        If Len(invoiceType) = 0 And Len(countryID) = 0 And Len(partnerID) = 0 And Len(invoicePenaltyID) = 0 And Len(invoiceDateFrom) = 0 And Len(invoiceDateTo) = 0 And Len(paymentDateFrom) = 0 And Len(paymentDateTO) = 0 And Len(regnr) = 0 And Len(vinNumber) = 0 Then 
            countryID = Request.QueryString("countryID")
            partnerID = Request.QueryString("partnerID")
            invoicePenaltyID = Request.QueryString("invoicePenaltyID")
            invoiceDateFrom = Request.QueryString("invoiceDateFrom")
            paymentDateTO = Request.QueryString("paymentDateTo")
            invoiceDateFrom = Request.QueryString("invoiceDateFrom")
            paymentDateTO = Request.QueryString("paymentDateTo")
            regnr = Request.QueryString("regnr")
            vinNumber = Request.QueryString("vinNumber")
        End IF
        If Len(countryID) > 0 Then 
            countryID = CLng(countryID)
            sqlCountryID = " And v.countryID = " & countryID & ""
        End IF
        If Len(partnerID) > 0 Then 
            partnerID = CLng(partnerID)
            sqlPartnerID = " And v.partnerID = " & partnerID & ""
        End IF
        If Len(invoiceDateFrom) > 0 And Len(invoiceDateTo) > 0 Then 
            sqlInvoiceDate = " And i.invoiceDate BETWEEN '" & invoiceDateFrom & "' AND '" & invoiceDateTo & "'"
        End IF
        If Len(paymentDateFrom) > 0 And Len(paymentDateTO) > 0 Then 
            sqlPaymentDate = " And i.paymentDate BETWEEN '" & paymentDateFrom & "' AND '" & paymentDateTO & "'"
        End IF
        If Len(regnr) > 0 Then 
            regnr = Trim(regnr)
            regnr = Replace(CStr(regnr), "'", "")
            regnr = Replace(CStr(regnr), """", "")
            regnr = Replace(CStr(regnr), "<", "")
            regnr = Replace(CStr(regnr), ">", "")
            regnr = Replace(CStr(regnr), "=", "")
            sqlRegnr = " And v.regnr Like '%" & regnr & "%'"
        End IF
        If Len(vinNumber) > 0 Then 
            vinNumber = Trim(vinNumber)
            vinNumber = Replace(CStr(vinNumber), "'", "")
            vinNumber = Replace(CStr(vinNumber), """", "")
            vinNumber = Replace(CStr(vinNumber), "<", "")
            vinNumber = Replace(CStr(vinNumber), ">", "")
            vinNumber = Replace(CStr(vinNumber), "=", "")
            sqlVinNumber = " And v.vinNumber Like '%" & vinNumber & "%'"
        End IF
        If Len(subDo) = 0 Then 
            sqlVehicleID = " And i.vehicleID < 0"
        End IF
        If invoiceType = CStr(1) Then 
            If Len(invoicePenaltyID) > 0 Then 
                invoicePenaltyID = CLng(invoicePenaltyID)
                sqlInvoicePenaltyID = " And (i.invoiceDurationPenaltyID = " & invoicePenaltyID & " Or ic.invoiceCreditDurationPenaltyID = " & invoicePenaltyID & ")"
                sqlInvoiceCreditPenaltyID = " And (i.invoiceCreditDurationPenaltyID = " & invoicePenaltyID & " Or i.invoiceDurationPenaltyID = " & invoicePenaltyID & ")"
            End IF
            sqlQuery = "Select i.invoiceDurationPenaltyID As invoiceID, i.invoiceAmount, i.invoiceClaim, i.invoiceDate, i.expiryDate, i.paymentDate, i.invoicePrinted, i.invoiceSettled, v.vehicleID, v.regnr, v.vinNumber, p.partnerName, co.country, co.countryCurrency, co.decimals, COUNT(ic.invoiceDurationPenaltyID) AS invoiceCreditCount From ((((tblInvoiceDurationPenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry co ON co.countryID = v.countryID) LEFT JOIN tblInvoiceCreditDurationPenalty ic ON ic.invoiceDurationPenaltyID = i.invoiceDurationPenaltyID) Where Len(i.invoiceDurationPenaltyID) > 0 " & sqlCountryID & sqlPartnerID & sqlInvoicePenaltyID & sqlInvoiceDate & sqlPaymentDate & sqlRegnr & sqlVinNumber & sqlVehicleID & " Group By i.invoiceDurationPenaltyID, i.invoiceAmount, i.invoiceClaim, i.invoiceDate, i.expiryDate, i.paymentDate, i.invoicePrinted, i.invoiceSettled, v.vehicleID, v.regnr, v.vinNumber, p.partnerName, co.country, co.countryCurrency, co.decimals Order By i.invoiceDurationPenaltyID"
            sqlQueryCredit = "Select i.invoiceCreditDurationPenaltyID As invoiceID, i.invoiceAmount, i.invoiceClaim, i.invoiceDate, i.expiryDate, i.paymentDate, i.invoicePrinted, i.invoiceSettled, v.vehicleID, v.regnr, v.vinNumber, p.partnerName, co.country, co.countryCurrency, co.decimals From (((tblInvoiceCreditDurationPenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry co ON co.countryID = v.countryID) Where Len(i.invoiceCreditDurationPenaltyID) > 0 " & sqlCountryID & sqlPartnerID & sqlInvoiceCreditPenaltyID & sqlInvoiceDate & sqlPaymentDate & sqlRegnr & sqlVinNumber & sqlVehicleID & " Order By i.invoiceDurationPenaltyID"
        ElseIf invoiceType = CStr(2) Then 
            If Len(invoicePenaltyID) > 0 Then 
                invoicePenaltyID = CLng(invoicePenaltyID)
                sqlInvoicePenaltyID = " And (i.invoiceDistancePenaltyID = " & invoicePenaltyID & " Or ic.invoiceCreditDistancePenaltyID = " & invoicePenaltyID & ")"
                sqlInvoiceCreditPenaltyID = " And (i.invoiceCreditDistancePenaltyID = " & invoicePenaltyID & " Or i.invoiceDistancePenaltyID = " & invoicePenaltyID & ")"
            End IF
            sqlQuery = "Select i.invoiceDistancePenaltyID As invoiceID, i.invoiceAmount, i.invoiceClaim, i.invoiceDate, i.expiryDate, i.paymentDate, i.invoicePrinted, i.invoiceSettled, v.vehicleID, v.regnr, v.vinNumber, p.partnerName, co.country, co.countryCurrency, co.decimals, COUNT(ic.invoiceDistancePenaltyID) AS invoiceCreditCount From ((((tblInvoiceDistancePenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry co ON co.countryID = v.countryID) LEFT JOIN tblInvoiceCreditDistancePenalty ic ON ic.invoiceDistancePenaltyID = i.invoiceDistancePenaltyID) Where Len(i.invoiceDistancePenaltyID) > 0" & sqlCountryID & sqlPartnerID & sqlInvoicePenaltyID & sqlInvoiceDate & sqlPaymentDate & sqlRegnr & sqlVinNumber & sqlVehicleID & " Group By i.invoiceDistancePenaltyID, i.invoiceAmount, i.invoiceClaim, i.invoiceDate, i.expiryDate, i.paymentDate, i.invoicePrinted, i.invoiceSettled, v.vehicleID, v.regnr, v.vinNumber, p.partnerName, co.country, co.countryCurrency, co.decimals Order By i.invoiceDistancePenaltyID"
            sqlQueryCredit = "Select i.invoiceCreditDistancePenaltyID As invoiceID, i.invoiceAmount, i.invoiceClaim, i.invoiceDate, i.expiryDate, i.paymentDate, i.invoicePrinted, i.invoiceSettled, v.vehicleID, v.regnr, v.vinNumber, p.partnerName, co.country, co.countryCurrency, co.decimals From (((tblInvoiceCreditDistancePenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry co ON co.countryID = v.countryID) Where Len(i.invoiceCreditDistancePenaltyID) > 0 " & sqlCountryID & sqlPartnerID & sqlInvoiceCreditPenaltyID & sqlInvoiceDate & sqlPaymentDate & sqlRegnr & sqlVinNumber & sqlVehicleID & " Order By i.invoiceDistancePenaltyID"
        End IF
        totInvoiceAmount = 0
        totInvoiceClaim = 0
        Response.Write("<tr bgcolor='#eeeeee'>")
        Response.Write("<td class='formBold'>Invoice ID</td>")
        Response.Write("<td class='formBold'>Partner</td>")
        Response.Write("<td class='formBold'>Licence number</td>")
        Response.Write("<td class='formBold'>VIN number</td>")
        Response.Write("<td class='formBold' align='right'>Invoice Amount (Tax excl.)</td>")
        Response.Write("<td class='formBold' align='right'>Invoice claim (Tax excl.)</td>")
        Response.Write("<td class='formBold' align='right'>Invoice Date</td>")
        Response.Write("<td class='formBold' align='right'>Expiry date</td>")
        Response.Write("<td class='formBold' align='right'>Days</td>")
        Response.Write("<td class='formBold' align='right'>Payment Date</td>")
        Response.Write("<td class='formBold' align='right'>Status</td>")
        Response.Write("<td class='formBold' align='right'>Country</td>")
        Response.Write("<td class='formBold' align='right'></td>")
        Response.Write("<td class='formBold' align='right'></td>")
        Response.Write("</tr>")
        If Len(invoiceType) > 0 Then 
            dbOpen()
            RecSet = Conn.Execute("" & sqlQuery & "")
            If Not RecSet.EOF Then 
                Do Until RecSet.EOF
                    invoiceID = RecSet.Fields("invoiceID").Value
                    invoiceAmount = RecSet.Fields("invoiceAmount").Value
                    invoiceClaim = RecSet.Fields("invoiceClaim").Value
                    invoiceDate = RecSet.Fields("invoiceDate").Value
                    expiryDate = RecSet.Fields("expiryDate").Value
                    paymentDate = RecSet.Fields("paymentDate").Value
                    invoicePrinted = RecSet.Fields("invoicePrinted").Value
                    invoiceSettled = RecSet.Fields("invoiceSettled").Value
                    vehicleID = RecSet.Fields("vehicleID").Value
                    regnr = RecSet.Fields("regnr").Value
                    vinNumber = RecSet.Fields("vinNumber").Value
                    partnerName = RecSet.Fields("partnerName").Value
                    country = RecSet.Fields("country").Value
                    countryCurrency = RecSet.Fields("countryCurrency").Value
                    decimals = RecSet.Fields("decimals").Value
                    invoiceCreditCount = RecSet.Fields("invoiceCreditCount").Value
                    invoiceCount = invoiceCount + 1
                    invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                    invoiceAmount = CDbl(invoiceAmount)
                    invoiceClaim = FormatNumber(CDbl(invoiceClaim), decimals)
                    invoiceClaim = CDbl(invoiceClaim)
                    totInvoiceAmount = totInvoiceAmount + invoiceAmount
                    totInvoiceClaim = totInvoiceClaim + invoiceClaim
                    If IsDBNull(invoiceDate) Then 
                        invoiceStatus = "<font color='red'>not activated</font>"
                    ElseIf CInt(invoiceCreditCount) = 1 Then 
                        invoiceStatus = "<font color='red'>Credited</font>"
                    ElseIf CDbl(invoiceSettled) = 1 Then 
                        invoiceStatus = "<font color='green'>settled</font>"
                    ElseIf CDbl(invoicePrinted) = 0 Then 
                        invoiceStatus = "queued for printing"
                    ElseIf CDbl(invoicePrinted) = 1 Then 
                        invoiceStatus = "printed"
                    End IF
                    If IsDBNull(invoiceDate) Then 
                        invoiceDate = "N/A"
                    End IF
                    If IsDBNull(expiryDate) = false And IsDBNull(paymentDate) = true Then 
                        expiryDays = DateDiff("d", Now(), Convert.ToDateTime(expiryDate))
                        If expiryDays >= 0 Then 
                            expiryDays = "<font color='green'>" & expiryDays & "</font>"
                        Else
                            expiryDays = "<font color='red'>" & expiryDays & "</font>"
                        End IF
                    ElseIf IsDBNull(expiryDate) = true Then 
                        expiryDate = "N/A"
                        expiryDays = "N/A"
                    Else
                        expiryDays = "N/A"
                    End IF
                    If IsDBNull(paymentDate) Then 
                        paymentDate = "N/A"
                    End IF
                    If rowNumber = 0 Then 
                        strGrey = " bgcolor='#eeeeee'"
                        rowNumber = 1
                    Else
                        strGrey = ""
                        rowNumber = 0
                    End IF
                    Response.Write("<tr" & strGrey & ">")
                    Response.Write("<td>" & invoiceID & "</td>")
                    Response.Write("<td>" & partnerName & "</td>")
                    Response.Write("<td>" & regnr & "</td>")
                    Response.Write("<td>" & vinNumber & "</td>")
                    Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceAmount), 2) & " " & countryCurrency & "</td>")
                    Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceClaim), 2) & " " & countryCurrency & "</td>")
                    Response.Write("<td align='right'>" & invoiceDate & "</td>")
                    Response.Write("<td align='right'>" & expiryDate & "</td>")
                    Response.Write("<td align='right'>" & expiryDays & "</td>")
                    Response.Write("<td align='right'>" & paymentDate & "</td>")
                    Response.Write("<td align='right'>" & invoiceStatus & "</td>")
                    Response.Write("<td align='right'>" & country & "</td>")
                    Response.Write("<td align='right'><a href='editVehicle.aspx?vehicleID=" & vehicleID & "'>view details</a></td>")
                    If invoiceType = CStr(1) Then 
                        Response.Write("<td align='right'><a href='printInvoiceDurationPenalty.aspx?invoiceID=" & invoiceID & "' target='_blank'>print invoice</a></td>")
                    ElseIf invoiceType = CStr(2) Then 
                        Response.Write("<td align='right'><a href='printInvoiceDistancePenalty.aspx?invoiceID=" & invoiceID & "' target='_blank'>print invoice</a></td>")
                    End IF
                    Response.Write("</tr>")
                    RecSet.MoveNext()
                Loop
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
            Response.Flush()
            invoiceCreditCount = 0
            dbOpen()
            RecSet = Conn.Execute("" & sqlQueryCredit & "")
            If Not RecSet.EOF Then 
                Do Until RecSet.EOF
                    invoiceID = RecSet.Fields("invoiceID").Value
                    invoiceAmount = RecSet.Fields("invoiceAmount").Value
                    invoiceClaim = RecSet.Fields("invoiceClaim").Value
                    invoiceDate = RecSet.Fields("invoiceDate").Value
                    expiryDate = RecSet.Fields("expiryDate").Value
                    paymentDate = RecSet.Fields("paymentDate").Value
                    invoicePrinted = RecSet.Fields("invoicePrinted").Value
                    invoiceSettled = RecSet.Fields("invoiceSettled").Value
                    vehicleID = RecSet.Fields("vehicleID").Value
                    regnr = RecSet.Fields("regnr").Value
                    vinNumber = RecSet.Fields("vinNumber").Value
                    partnerName = RecSet.Fields("partnerName").Value
                    country = RecSet.Fields("country").Value
                    countryCurrency = RecSet.Fields("countryCurrency").Value
                    decimals = RecSet.Fields("decimals").Value
                    invoiceCount = invoiceCount + 1
                    invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                    invoiceAmount = CDbl(invoiceAmount)
                    invoiceClaim = FormatNumber(CDbl(invoiceClaim), decimals)
                    invoiceClaim = CDbl(invoiceClaim)
                    totInvoiceAmount = CDbl(totInvoiceAmount) - CDbl(invoiceAmount)
                    totInvoiceClaim = CDbl(totInvoiceClaim) - CDbl(invoiceClaim)
                    If IsDBNull(invoiceDate) Then 
                        invoiceStatus = "<font color='red'>not activated</font>"
                    ElseIf CInt(invoiceCreditCount) = 1 Then 
                        invoiceStatus = "<font color='red'>Credited</font>"
                    ElseIf CDbl(invoiceSettled) = 1 Then 
                        invoiceStatus = "<font color='green'>settled</font>"
                    ElseIf CDbl(invoicePrinted) = 0 Then 
                        invoiceStatus = "queued for printing"
                    ElseIf CDbl(invoicePrinted) = 1 Then 
                        invoiceStatus = "printed"
                    End IF
                    If IsDBNull(invoiceDate) Then 
                        invoiceDate = "N/A"
                    End IF
                    If IsDBNull(expiryDate) = false And IsDBNull(paymentDate) = true Then 
                        expiryDays = DateDiff("d", Now(), Convert.ToDateTime(expiryDate))
                        If expiryDays >= 0 Then 
                            expiryDays = "<font color='green'>" & expiryDays & "</font>"
                        Else
                            expiryDays = "<font color='red'>" & expiryDays & "</font>"
                        End IF
                    ElseIf IsDBNull(expiryDate) = true Then 
                        expiryDate = "N/A"
                        expiryDays = "N/A"
                    Else
                        expiryDays = "N/A"
                    End IF
                    If IsDBNull(paymentDate) Then 
                        paymentDate = "N/A"
                    End IF
                    If rowNumber = 0 Then 
                        strGrey = " bgcolor='#eeeeee'"
                        rowNumber = 1
                    Else
                        strGrey = ""
                        rowNumber = 0
                    End IF
                    Response.Write("<tr" & strGrey & ">")
                    Response.Write("<td>" & invoiceID & "</td>")
                    Response.Write("<td>" & partnerName & "</td>")
                    Response.Write("<td>" & regnr & "</td>")
                    Response.Write("<td>" & vinNumber & "</td>")
                    Response.Write("<td align='right'>-" & FormatNumber(CDbl(invoiceAmount), 2) & " " & countryCurrency & "</td>")
                    Response.Write("<td align='right'>-" & FormatNumber(CDbl(invoiceClaim), 2) & " " & countryCurrency & "</td>")
                    Response.Write("<td align='right'>" & invoiceDate & "</td>")
                    Response.Write("<td align='right'>" & expiryDate & "</td>")
                    Response.Write("<td align='right'>" & expiryDays & "</td>")
                    Response.Write("<td align='right'>" & paymentDate & "</td>")
                    Response.Write("<td align='right'>" & invoiceStatus & "</td>")
                    Response.Write("<td align='right'>" & country & "</td>")
                    Response.Write("<td align='right'><a href='editVehicle.aspx?vehicleID=" & vehicleID & "'>view details</a></td>")
                    If invoiceType = CStr(1) Then 
                        Response.Write("<td align='right'><a href='printInvoiceCreditDurationPenalty.aspx?invoiceID=" & invoiceID & "' target='_blank'>print invoice</a></td>")
                    ElseIf invoiceType = CStr(2) Then 
                        Response.Write("<td align='right'><a href='printInvoiceCreditDistancePenalty.aspx?invoiceID=" & invoiceID & "' target='_blank'>print invoice</a></td>")
                    End IF
                    Response.Write("</tr>")
                    RecSet.MoveNext()
                Loop
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
            If invoiceCount = 0 Then 
                rowNumber = 0
                Response.Write("<tr><td><font color='red'>No invoices found.</font></td></tr>")
            End IF
            If rowNumber = 0 Then 
                strGrey = " bgcolor='#eeeeee'"
                rowNumber = 1
            Else
                strGrey = ""
                rowNumber = 0
            End IF
            Response.Write("<tr" & strGrey & ">")
            Response.Write("<td class='formBold' colspan='4'>Total sum:</td>")
            Response.Write("<td class='formBold' align='right'>" & FormatNumber(CDbl(totInvoiceAmount), 2) & " EUR</td>")
            Response.Write("<td class='formBold' align='right'>" & FormatNumber(CDbl(totInvoiceClaim), 2) & " EUR</td>")
            Response.Write("<td class='formBold' colspan='8'></td>")
            Response.Write("</tr>")
        Else
            Response.Write("<tr><td><font color='red'>No invoices found.</font></td></tr>")
        End IF
        Response.Write("<tr><td colspan='14'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Under()
    End IF
%>
